package com.example.phone.search;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.phone.ProductDetails.Detail;
import com.example.phone.R;
import com.example.phone.base.RequestInterface;
import com.example.phone.bean.SearchResult;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class SearchResultAdapter extends RecyclerView.Adapter<SearchResultAdapter.mViewHolder> {

    private List<SearchResult> searchResults;
    private Context mContext;

    static class mViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.product_image)
        ImageView productImage;
        @BindView(R.id.product_name)
        TextView productName;
        @BindView(R.id.subtitle)
        TextView subtitle;
        @BindView(R.id.product_price)
        TextView productPrice;
        @BindView(R.id.ll_search_item)
        LinearLayout llSearchItem;

        mViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public SearchResultAdapter(Context context, List<SearchResult> list) {
        searchResults = list;
        mContext = context;
    }


    @Override
    public mViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.search_detail_row, viewGroup, false);

        return new mViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final mViewHolder viewHolder, int i) {

        final SearchResult result = searchResults.get(i);

        viewHolder.productName.setText(result.getName());
        viewHolder.subtitle.setText(result.getSubtitle());
        viewHolder.productPrice.setText(mContext.getString(R.string.rmb_price, result.getPrice()));

        Glide.with(mContext)
                .load(RequestInterface.baseImgPath + result.getMainImage())
                .into(viewHolder.productImage);
        viewHolder.llSearchItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, Detail.class);
                intent.putExtra("productId", result.getId());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return searchResults.size();
    }

}
